package com.health.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.common.entity.SetMeal;
import com.health.common.entity.SetMealWithGroup;
import com.health.common.vo.QueryPageBean;
import com.health.common.vo.RespPageBean;
import com.health.mapper.SetMealMapper;
import com.health.mapper.SetMealWithGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealWithGroupMapper setMealWithGroupMapper;

    public RespPageBean getAllSetMealByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Page<SetMeal> page = new Page<>(currentPage,pageSize);
        QueryWrapper<SetMeal> wrapper = new QueryWrapper<>();
        if(queryString != null){
            wrapper.and(w -> w.like("name",queryString).or().like("code",queryString).or().like("helpCode",queryString));
        }
        IPage<SetMeal> iPage = setMealMapper.selectPage(page, wrapper);

        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(iPage.getTotal());
        respPageBean.setData(iPage.getRecords());
        return respPageBean;
    }

    public List<Integer> getGroupIdsById(Integer id) {
        return setMealMapper.getGroupIdsById(id);
    }

    @Transactional
    public Integer addSetMeal(SetMeal setMeal,Integer[] items) {
        int insert = setMealMapper.insert(setMeal);
        Integer setMealId4DB = setMeal.getId();
        SetMealWithGroup setMealWithGroup = new SetMealWithGroup();
        for (Integer item : items) {
            setMealWithGroup.setSetMealId(setMealId4DB);
            setMealWithGroup.setCheckGroupId(item);
            setMealWithGroupMapper.insert(setMealWithGroup);
        }
        return insert;
    }

    @Transactional
    public Integer updateSetMeal(SetMeal setMeal, Integer[] items) {
        int i = setMealMapper.updateById(setMeal);
        //更新套餐检查组关联表，先删除再重新插入数据
        QueryWrapper<SetMealWithGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setMeal.getId());
        setMealWithGroupMapper.delete(wrapper);
        SetMealWithGroup setMealWithGroup = new SetMealWithGroup();
        setMealWithGroup.setSetMealId(setMeal.getId());
        for (Integer item : items) {
            setMealWithGroup.setCheckGroupId(item);
            setMealWithGroupMapper.insert(setMealWithGroup);
        }
        return i;
    }

    @Transactional
    public Integer deleteSetMealById(Integer id) {
        //删除套餐表，同时删除套餐检查组关联表
        int i = setMealMapper.deleteById(id);
        QueryWrapper<SetMealWithGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        setMealWithGroupMapper.delete(wrapper);
        return i;
    }
}
